package com.ikingtech.platform.service.sms.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.sms.model.SmsQueryParamDTO;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.sms.entity.SmsDO;
import com.ikingtech.platform.service.sms.mapper.SmsMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tie yan
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SmsService extends ServiceImpl<SmsMapper, SmsDO> {

    public boolean nameExist(String name) {
        return this.baseMapper.exists(Wrappers.<SmsDO>lambdaQuery().isNotNull(SmsDO::getId).eq(SmsDO::getName, name));
    }

    public boolean appIdExist(String appId) {
        return this.baseMapper.exists(Wrappers.<SmsDO>lambdaQuery().isNotNull(SmsDO::getId).eq(SmsDO::getAppId, appId));
    }

    public boolean nameExist(String smsId, String name) {
        return this.baseMapper.exists(Wrappers.<SmsDO>lambdaQuery().ne(SmsDO::getCode, smsId).eq(SmsDO::getName, name));
    }

    public boolean appIdExist(String smsId, String appId) {
        return this.baseMapper.exists(Wrappers.<SmsDO>lambdaQuery().ne(SmsDO::getCode, smsId).eq(SmsDO::getAppId, appId));
    }

    public boolean exist(String id) {
        return this.baseMapper.exists(Wrappers.<SmsDO>lambdaQuery().eq(SmsDO::getId, id));
    }

    public PageResult<SmsDO> listPage(SmsQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<SmsDO>lambdaQuery()
                .eq(Tools.Str.isNotBlank(queryParam.getCode()), SmsDO::getCode, queryParam.getCode())
                .like(Tools.Str.isNotBlank(queryParam.getName()), SmsDO::getName, queryParam.getName())
                .eq(Tools.Str.isNotBlank(queryParam.getAppId()), SmsDO::getAppId, queryParam.getAppId())));
    }

    public List<SmsDO> listBySupportTemplate(Boolean supportTemplate) {
        return this.list(Wrappers.<SmsDO>lambdaQuery().eq(null != supportTemplate, SmsDO::getSupportTemplate, supportTemplate));
    }
}
